package com.example.onlinetaxi.view.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.OrderCallBack.ReceivedOrderCallBack;
import com.example.onlinetaxi.dto.OrderDto.ReceivedOrderDto;
import com.example.onlinetaxi.util.TimeUtil;

import java.util.List;

public class DriverDoing1OrderAdapter extends RecyclerView.Adapter<DriverDoing1OrderAdapter.ViewHolder> {
    // 用于构造页面的订单列表
    private List<ReceivedOrderDto> driverDoing1OrderList;
    // 点击事件回调函数
    private ReceivedOrderCallBack receivedOrderCallBack;

    // 构造函数
    public DriverDoing1OrderAdapter(List<ReceivedOrderDto> driverDoing1OrderList) {
        this.driverDoing1OrderList = driverDoing1OrderList;
    }

    /**
     * 内部类也继承其内部类，并重写构造器
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout driverDoing1OrderAllInfo;
        TextView driverDoing1OrderId;
        TextView driverDoing1OrderUseCarTime;
        // 订单类型（实时单/预约单）
        TextView driverDoing1OrderUseCarType;
        TextView driverDoing1OrderStartInfo;
        TextView driverDoing1OrderEndInfo;
        TextView driverDoing1OrderTime;
        TextView driverDoing1OrderDistance;
        TextView driverDoing1OrderCost;
        TextView driverDoing1OrderCreateTime;
        Button cancelButton;

        /**
         * 内部类构造器
         *
         * @param view Recycler子项的最外层布局
         */
        public ViewHolder(View view) {
            super(view);
            // 布局
            driverDoing1OrderAllInfo = view.findViewById(R.id.driver_doing_1_order_all_info);  // 点击后进入等待接单界面
            // 按钮
            cancelButton = view.findViewById(R.id.driver_doing_1_order_cancel_btn);
            // 文本框
            driverDoing1OrderId = view.findViewById(R.id.driver_doing_1_order_id);
            driverDoing1OrderUseCarTime = view.findViewById(R.id.driver_doing_1_order_use_car_time);
            driverDoing1OrderUseCarType = view.findViewById(R.id.driver_doing_1_order_use_car_type);
            driverDoing1OrderStartInfo = view.findViewById(R.id.driver_doing_1_order_start_info);
            driverDoing1OrderEndInfo = view.findViewById(R.id.driver_doing_1_order_end_info);
            driverDoing1OrderTime = view.findViewById(R.id.driver_doing_1_order_time);
            driverDoing1OrderDistance = view.findViewById(R.id.driver_doing_1_order_distance);
            driverDoing1OrderCost = view.findViewById(R.id.driver_doing_1_order_cost);
            driverDoing1OrderCreateTime = view.findViewById(R.id.driver_doing_1_order_create_time);
        }
    }

    // 第二个被回调，创建一个ViewHolder对象返回
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_driver_doing_1_order, parent, false);
        // 布局传递到内部类的构造函数中
        ViewHolder holder = new ViewHolder(view);
        // 返回内部类对象
        return holder;
    }

    // 子项数据赋值，通过这个方法实现滑动添加，第三个被回调
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 得到当前order
        ReceivedOrderDto order = driverDoing1OrderList.get(position);

        // 放入本地存储
        if (!order.getId().toString().equals("")) {
            LocalStorage.currentOrderId = order.getId().toString();
        } else {
            LocalStorage.currentOrderId = null;
        }

        holder.driverDoing1OrderAllInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 使用回调函数跳转等待接单
                receivedOrderCallBack.jump(order);
            }
        });
        holder.cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 使用回调函数发送请求，删除当前订单
                receivedOrderCallBack.clickButton(order);
            }
        });
        // 设置订单id
        holder.driverDoing1OrderId.setText("订单id:" + order.getId());
        holder.driverDoing1OrderUseCarTime.setText("用车时间: " + TimeUtil.DateToString(order.getUseCarTime()));
        holder.driverDoing1OrderUseCarType.setText(order.getUseCarType());
        holder.driverDoing1OrderStartInfo.setText(order.getStartInfo());
        holder.driverDoing1OrderEndInfo.setText(order.getEndInfo());
        holder.driverDoing1OrderTime.setText("预计乘车时间:" + order.getTime() + "分钟");
        holder.driverDoing1OrderDistance.setText("预计乘车里程:" + order.getDistance() + "公里");
        holder.driverDoing1OrderCost.setText("预计收益：" + order.getCost() + " 元");
        holder.driverDoing1OrderCreateTime.setText("订单创建时间: " + TimeUtil.DateToString(order.getCreateTime()));
    }

    //  @return 获取长度，第一个被回调
    public int getItemCount() {
        return driverDoing1OrderList.size();
    }

    // 设置回调函数
    public void setReceivedOrderCallBack(ReceivedOrderCallBack receivedOrderCallBack) {
        this.receivedOrderCallBack = receivedOrderCallBack;
    }
}
